<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Grid{

	public $tablename;
	public $primary_key;
	public $CI;
	public $data;
	private $setting = array(
		'success' => 15,
		'add' => 1,
		'update' => 2
	);
	public $fields = array();
	public $relation = array('table'=>array(), 'id'=>array());
	public $columns = array();

	public function __construct($params){
		$this->CI =& get_instance();
		$this->tablename = $params['tablename'];
		$this->primary_key = $params['primary_key'];
		$this->where = null;
		if(isset($params['where']))
			$this->where = $params['where'];
		$this->data['update_btn'] = !isset($params['update']); 
		$this->data['delete_btn'] = !isset($params['delete']);
		$this->data['portlet_type'] = 'Data';
		$this->data['subject'] = "Default";
		$this->data['record_style'] = array();
	}

	public function add_subject($subject){
		$this->data['subject'] = $subject;
	}

	public function add_relation_field($field, $ref_table, $ref_id, $alias, $style, $attr, $format=false){
		$this->relation['table'][] = $ref_table;
		$this->relation['id'][] = $ref_id;
		$this->add_field($field, $alias, $style, $attr, $format);
	}

	public function add_field($field, $alias, $style, $attr, $format=false){
		$this->data['column'][$field] = $alias;
		$this->fields[] = $field;
		$this->data['record_style'][$field] = $style;
		$this->data['field_attr'][$field] = $attr;
		if($format)$this->data['format'][$field] = $format;
	}

	private function process(){ 
		$action = $this->get_action();

		switch ($action){
			case 1:				
				foreach ($this->fields as $field){
					$this->data[$field] = '';
				}
				$this->data['id'] = '';
				break;
			case 2:
				$where = array($this->primary_key => $this->CI->uri->segment(4));
		
				$this->CI->load->model('model');
				$result = $this->CI->model->get_data_by_id($this->tablename, $where);
				
				foreach ($this->fields as $field){
					$this->data[$field] = $result->$field;
				}
				$this->data['id'] = $this->CI->uri->segment(4);
				break;
			default:
				$this->data['fields'] = $this->fields;
				$this->data['id'] = $this->primary_key;
				
				$this->data['records'] = $this->load_data_to_grid();				
		}
	}
	
	private function load_data_to_grid(){
		$this->CI->load->model('model');
		$this->fields[] = $this->primary_key;
		return  $this->CI->model->get_data($this->tablename, implode(',',$this->fields), $this->relation, $this->where);
	}
	
	private function get_action(){
		$segment = $this->CI->uri->segment(3);
		switch ($segment){
			case 'add':
				if(!empty($_POST)){
					$result = $this->insert();
					$action = $this->setting[$result];
				}else{
					$action = $this->setting[$segment];
				}
				break;
			case 'update':
				if(!empty($_POST)){
					$result = $this->update();
					$action = $this->setting[$result];
				}else{
					$action = $this->setting[$segment];
				}
				break;
			default:
				$action = $this->setting['success'];
				break;
		}
		
		return $action;
	}
	
	private function insert(){
		$value = array();
		foreach ($this->fields as $field){
			$value[$field] = $this->CI->input->post($field);
			if($this->data['field_attr'][$field]=='hidden') $value[$field] = md5($value[$field]);
		}
		$this->CI->load->model('model');
		if($this->CI->model->insert($this->tablename, $value)=='1') return 'success';
		else return 'add';
	}
	
	private function update(){
		$value = array();
		foreach ($this->fields as $field){
			if($this->data['field_attr'][$field]=='show')
				$value[$field] = $this->CI->input->post($field);
		}
		$id = $this->CI->input->post('id');
		$this->CI->load->model('model');
		if($this->CI->model->update($this->tablename, $value)=='1') return 'success';
		else return 'update';
	}
	
	private function load(){
		if($this->CI->session->userdata('admin')){
			$session_data = $this->CI->session->userdata('admin');
			$this->data['username'] = $session_data['username'];
			$this->data['nama'] = $session_data['nama'];
			$this->data['type'] = $session_data['type'];
			$this->process();
			$this->CI->load->view('main', $this->data);
		}else{
			redirect('login', 'refresh');
		}
	}

	public function show(){
		$action = $this->get_action();
		
		switch ($action){
		case 1:			
			$method = $this->CI->uri->segment(2);
			if(!$method) $method = 'index';
			$this->data['action'] = $this->CI->uri->segment(1).'/'.$method.'/add';
			$this->data['form'] = $this->form;
			$this->data['portlet_type'] = 'Form';
			$this->data['title'] = 'Home';
			$this->load();
			break;
		case 2:
			$method = $this->CI->uri->segment(2);
			if(!$method) $method = 'index';
			$this->data['action'] = $this->CI->uri->segment(1).'/'.$method.'/update';
			$this->data['form'] = $this->form;
			$this->data['portlet_type'] = 'Form';
			$this->data['title'] = 'Home';
			$this->load();
			break;
		default:
			$method = $this->CI->uri->segment(2);
			if(!$method) $method = 'index';
			$this->data['action_add'] = $this->CI->uri->segment(1).'/add';
			$this->data['action_edit'] = $this->CI->uri->segment(1).'/update';
			$this->data['action_delete'] = $this->CI->uri->segment(1).'/delete';
			$this->data['title'] = 'Home';
			$this->load();
			break;
		}
	}
	
	private function get_segment(){
		$this->data['action'] = $this->get_action();
	}
	
	private function get_status_info(){
		if($this->CI->uri->segment(3)==null){
			return 15;
		}
	}
	
	public function set_menu($menu){
		$this->data['menu'] = $menu;
	}
	
	public function set_error($error){
		$this->data['error'] = $error;
	}
	
	public function set_portlet_style($style){
		$this->data['portlet_style'] = $style;
	}
	
	public function set_portlet_title($title){
		$this->data['portlet_title'] = $title;
	}
	
	public function set_form($form){
		$this->form = $form;
	}
}